package Thread;

/**
 * @program: javaLearn
 * @description:
 * @author: renhai
 * @Date ：2021/12/21 23:47
 **/
public class MultiWithDraw extends Thread {
    private Account account;
    private int amount;

    public MultiWithDraw(Account account, int amount) {
        this.account = account;
        this.amount = amount;
    }

    public void run() {
        synchronized (account) {
            String str = Thread.currentThread().getName() + "取款前余额:" + account.getBalance();
            try {
                sleep(5); // 延长取款时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(str + "  取款：" + account.withDraw(amount) + "	取款后余额：" + account.getBalance());
        }
    }

    public static void main(String[] args) {
        Account a = new Account(500);
        for (int i = 1; i <= 10; i++) {
            (new MultiWithDraw(a, 10 + i)).start();
        }

    }
}
